package com.cxmx.escort.common.utils.result;


import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class OutDataUtil {


    public static void writeData(Object dataObject, String info, String status, HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<>();
        //rows：传入的数据 info:操作信息（成功或者失败） status：操作返回的状态
        resultMap.put("data", dataObject);
        resultMap.put("msg", info);
        resultMap.put("code", status);
        writeHTML(response, JSON.toJSONString(resultMap));
    }

    /**
     * 页面打印
     *
     * @param res
     * @param content
     */
    public static void writeHTML(HttpServletResponse res, String content) {
        res.setCharacterEncoding("utf-8");
        res.setContentType("text/json");
        try {
            PrintWriter out = res.getWriter();
            out.write(content);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 页面打印
     *
     * @param res
     * @param content
     */
    public static void write(HttpServletResponse res, String content) {
        res.setCharacterEncoding("utf-8");
        res.setContentType("text/json");
        try {
            String s = JsonUtil.parseSuccessJson(content);
            PrintWriter out = res.getWriter();
            out.write(s);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 页面打印JSON
     *
     * @param res
     * @param obj
     * @param msg
     * @param status
     */
    public static void writeJSON(HttpServletResponse res, Object obj, String msg, int status) {
        writeHTML(res, JsonUtil.parseJson(obj, msg, status));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     */
    public static void writeSuccessJSON(HttpServletResponse res) {
        writeHTML(res, JsonUtil.parseSuccessJson(null, ResultCode.SUCCESS.getMsg()));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     * @param obj
     */
    public static void writeSuccessJSON(HttpServletResponse res, Object obj) {
        writeHTML(res, JsonUtil.parseSuccessJson(obj, ResultCode.SUCCESS.getMsg()));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     * @param obj
     * @param msg
     */
    public static void writeSuccessJSON(HttpServletResponse res, String msg, Object obj) {
        writeHTML(res, JsonUtil.parseSuccessJson(obj, msg));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     */
    public static void writeFailJSON(HttpServletResponse res) {
        writeHTML(res, JsonUtil.parseFailJson(null, ResultCode.FAIL.getMsg()));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     * @param msg
     */
    public static void writeFailJSON(HttpServletResponse res, String msg) {
        writeHTML(res, JsonUtil.parseFailJson(null, msg));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     * @param msg
     */
    public static void writeFailJSON(HttpServletResponse res, String msg, int status) {
        writeHTML(res, JsonUtil.parseFailJson(null, msg, status));
    }


    /**
     * 页面打印JSON
     *
     * @param res
     * @param msg
     */
    public static void writeFailJSON(HttpServletResponse res, Object msg) {
        writeHTML(res, JsonUtil.parseFailJson(null, msg));
    }

    /**
     * 页面打印JSON
     *
     * @param res
     * @param obj
     * @param msg
     */
    public static void writeFailJSON(HttpServletResponse res, String msg, Object obj) {
        writeHTML(res, JsonUtil.parseFailJson(obj, msg));
    }


    /**
     * 页面打印map json
     *
     * @param res
     * @param map
     */
    public static void writeMapJSON(HttpServletResponse res, Map<String, Object> map) {
        writeHTML(res, JsonUtil.parseMapJson(map));
    }

    /**
     * 页面打印list json
     *
     * @param res
     * @param list
     */
    public static void writeListJSON(HttpServletResponse res, List<Map<String, Object>> list) {
        writeHTML(res, JsonUtil.parseListJson(list));
    }


    /**
     * 输出任意类型的list类型
     * 页面打印list json
     *
     * @param res
     * @param list
     */
    public static void writeList(HttpServletResponse res, List list) {
        writeHTML(res, JsonUtil.parseListJsonNew(list));
    }


}
